package com.smfx.yxt.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.smfx.yxt.daoSqlserver.ClassMonthPointsMapper;
import com.smfx.yxt.daoSqlserver.ClassTermPointsMapper;
import com.smfx.yxt.model.ClassMonthPoints;
import com.smfx.yxt.page.Page;
import com.smfx.yxt.reqVo.PointsReqVO;
import com.smfx.yxt.service.IClassPointService;
import com.smfx.yxt.view.PointItemView;
import com.smfx.yxt.view.PointTotalView;

@Service("classPointService")
public class ClassPointServiceImpl implements IClassPointService {

	@Autowired
	private ClassTermPointsMapper classTermPointsMapper;
	@Autowired
	private ClassMonthPointsMapper classMonthPointsMapper;
	
	@Override
	public int addClassMonthPoints(PointsReqVO vo) {
		return classMonthPointsMapper.addClassMonthPoints(vo);
	}

	@Override
	public int addClassTermPoints(PointsReqVO vo) {
		return classTermPointsMapper.addClassTermPoints(vo);
	}

	@Override
	public int updateClassTermPoints(PointsReqVO vo) {
		return classTermPointsMapper.updateClassTermPoints(vo);
	}

	@Override
	public int updateClassTotalPoints(PointsReqVO vo) {
		return classTermPointsMapper.updateClassTotalPoints(vo);
	}

	@Override
	public int updateClassMonthPoints(Integer classId, Float point) {
		int result = startUpdatePoints(classId, point);
		if(result == 0) {
			for(int i = 0; i < 2; i++) {
				result = startUpdatePoints(classId, point);
				if(result == 1) {
					break;
				}
			}
		}
		return result;
	}

	private int startUpdatePoints(Integer classId, Float point) {
		PointsReqVO vo = new PointsReqVO();
		vo.setKeyId(classId);
		ClassMonthPoints student = classMonthPointsMapper.selectClassForCurrMonth(vo);
		Float points = student.getPoints() == null ? 0 : student.getPoints();
		student.setPoints(points + point);
		return classMonthPointsMapper.updateForVersionById(student);
	}

	@Override
	public PointTotalView selectTotalPointForClass(PointsReqVO vo) {
		return classTermPointsMapper.selectTotalPointForClass(vo);
	}

	@Override
	public List<PointTotalView> selectTotalPointForClassByPage(Page<PointTotalView> page) {
		return classTermPointsMapper.selectTotalPointForClassByPage(page);
	}

	@Override
	public List<PointItemView> selectPointItemForClassByPage(Page<PointItemView> page) {
		return classTermPointsMapper.selectPointItemForClassByPage(page);
	}

}
